﻿#include "uideviceinfo.h"
#include "ElaPushButton.h"
#include <QPushButton>
#include <QPainter>

#include <shlib/quc_sdk/panelwidget/panelwidget.h>
#include <shlib/quc_sdk/iconhelper/icon_helper.h>
#include <shlib/cwebrequestapi.h>



UiDeviceInfo::UiDeviceInfo(QWidget *parent) :QWidget(parent) {
    initUi();
}

void UiDeviceInfo::initUi()
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setStyleSheet("background-color: rgba(0, 0, 0, 0);");

    IconHelper* iconsMgr = IconHelper::instance();

    QFrame* farme = new QFrame(this);

    m_newSceneBtn = new ElaPushButton("新建设备",farme);
    m_newSceneBtn->setObjectName("scene_manager_btn");
    m_newSceneBtn->setMinimumSize(100, 30);
    connect(m_newSceneBtn, &ElaPushButton::clicked, this, &UiDeviceInfo::addDevice);

    m_newSceneBtn->setIcon(QIcon(iconsMgr->getPixmap(QColor::fromString("#63E6BE"), IconHelper::FontAwesome6Solid, 0xf5ff, 22, 25,25)));

    m_refreshSceneBtn = new ElaPushButton("刷新列表",farme);
    m_refreshSceneBtn->setObjectName("scene_manager_btn");
    m_refreshSceneBtn->setStyleSheet("QPushButton { background-color: #1e3050; border: none; border-radius: 6px; color: white;}");
    m_refreshSceneBtn->setIconSize(QSize(25,25));
    m_refreshSceneBtn->setMinimumSize(100, 30);

    QHBoxLayout* btnLayout = new QHBoxLayout();
    btnLayout->setContentsMargins(0,0,0,40);
    btnLayout->addStretch(1);
    btnLayout->addWidget(m_newSceneBtn);
    btnLayout->addWidget(m_refreshSceneBtn);
    btnLayout->addStretch(0);

    farme->setLayout(btnLayout);
    farme->setFixedHeight(30);

    m_view = new PanelWidget;

    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->addWidget(farme);
    m_mainLayout->addWidget(m_view);
}

void UiDeviceInfo::slotSelectModelChange(const QString &modelname, const QString &user) {
    m_user = user;
    m_modelName = modelname;

    for(auto& key : m_deviceCardsMap.keys()) {
        m_deviceCardsMap[key]->setParent(nullptr);
        m_deviceCardsMap[key]->deleteLater();
    }
    m_deviceCardsMap.clear();

    QList<CRtDeviceInfo> devlist;
    if(!CWebRequestApi::ins().getDevicesByModel(m_user, m_modelName, devlist)) {
        ElaMessageBar::error(ElaMessageBarType::Top, "错误", "读取设备信息失败!",2000);
        return;
    }

    QList<QWidget *> cads;
    for(int i = 0; i < devlist.size(); ++i) {
        UiDeviceInfoCard *frm = new UiDeviceInfoCard(devlist[i], this);
        frm->setFixedHeight(330);
        frm->setMinimumWidth(661);
        frm->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        m_deviceCardsMap.insert(devlist[i].name, frm);
        cads.append(frm);
    }

    m_view->setWidget(cads, 2);
    m_view->setMargin(1);
    m_view->setSpace(3);
    m_view->setAutoWidth(true);
}

void UiDeviceInfo::addDevice() {
    QStringList groups;
    for(auto& it : m_deviceCardsMap) {
        if(!groups.contains(it->info().grouping)) {
            groups.push_back(it->info().grouping);
        }
    }

    CRtDeviceInfo info;
    UiDeviceInfoEdit w(UiDeviceInfoEdit::EDeviceAdd, &info, groups);
    UiCustomDialog* dialog = new UiCustomDialog;
    dialog->setWindowTitle("新建设备");
    dialog->setConten(&w);
    while(dialog->exec() == UiCustomDialog::Accepted) {
        if(!w.save()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "名字和描述不能为空!",2000);
            continue;
        }

        info.model_name = m_modelName;
        info.user = m_user;

        if(!CWebRequestApi::ins().addDevice(info)) {
            ElaMessageBar::warning(ElaMessageBarType::Top, "警告", "服务器请求失败,请稍后重试!",2000);
            break;
        }

        ElaMessageBar::success(ElaMessageBarType::Top, "提示", "添加设备成功!",2000);
        slotSelectModelChange(m_modelName, m_user);
        break;
    }
    dialog->deleteLater();
}
